<template>
  <div class="register-modal-mask">
    <div class="register-modal">
      <span class="close-btn" @click="$emit('close')">&times;</span>
      <h2 class="register-title">注册</h2>
      <form class="register-form" @submit.prevent="onRegister">
        <label class="register-label">用户名</label>
        <input
          v-model="username"
          class="register-input"
          type="text"
          placeholder="请输入用户名"
          required
          :disabled="isLoading"
        />
        <label class="register-label">密码</label>
        <input
          v-model="password"
          class="register-input"
          type="password"
          placeholder="请输入密码"
          required
          :disabled="isLoading"
        />
        <label class="register-label">确认密码</label>
        <input
          v-model="confirmPassword"
          class="register-input"
          type="password"
          placeholder="请再次输入密码"
          required
          :disabled="isLoading"
        />
        <button
          class="register-btn"
          type="submit"
          :disabled="isLoading"
          :class="{ loading: isLoading }"
        >
          {{ isLoading ? "注册中..." : "注册" }}
        </button>
      </form>
      <div class="register-footer">
        已有账号？
        <a class="login-link" @click="$emit('login')">立即登录</a>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { register } from "../api/user";

const username = ref("");
const password = ref("");
const confirmPassword = ref("");
const isLoading = ref(false);

const router = useRouter();
const emit = defineEmits(["close"]);

const onRegister = async () => {
  // 表单验证
  if (!username.value.trim()) {
    ElMessage.warning("请输入用户名");
    return;
  }
  if (!password.value.trim()) {
    ElMessage.warning("请输入密码");
    return;
  }
  if (password.value.length < 6) {
    ElMessage.warning("密码长度至少为6位");
    return;
  }
  if (password.value !== confirmPassword.value) {
    ElMessage.error("两次输入的密码不一致");
    return;
  }

  isLoading.value = true;
  try {
    const response = await register(username.value.trim(), password.value);

    if (response.code === 0) {
      ElMessage.success(response.msg || "注册成功");
      // 关闭注册弹窗
      emit("close");
      // 可以跳转到登录页面或直接触发登录
      ElMessage.success("请使用新注册的账号登录");
    } else {
      throw new Error(response.msg || "注册失败");
    }
  } catch (error) {
    console.error("注册失败:", error);
    // 统一错误处理
    let errorMsg = "注册失败，请重试";
    // 优先从error.response.data中获取错误信息
    if (error.response && error.response.data) {
      // 1. 首先检查是否有我们自定义的msg字段
      if (error.response.data.msg && error.response.data.msg !== "注册失败") {
        errorMsg = error.response.data.msg;
      }
      // 2. 其次检查username字段中的具体错误
      else if (
        error.response.data.data &&
        error.response.data.data.username &&
        error.response.data.data.username.length > 0
      ) {
        const usernameError = error.response.data.data.username[0];
        // 处理中英文混合的错误消息
        if (usernameError.includes("already exists")) {
          errorMsg = "该用户名已被注册";
        } else {
          errorMsg = usernameError;
        }
      }
    }
    ElMessage.error(errorMsg);
  } finally {
    isLoading.value = false;
  }
};
</script>

<style scoped>
.register-modal-mask {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.18);
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
}
.register-modal {
  position: relative;
  background: #fff;
  border-radius: 16px;
  padding: 38px 38px 32px 38px;
  max-width: 440px;
  width: 100%;
  box-shadow: 0 4px 32px rgba(140, 38, 230, 0.1);
  min-width: 320px;
  animation: popup-fade-in 0.2s;
}
@keyframes popup-fade-in {
  from {
    opacity: 0;
    transform: scale(0.96);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}
.close-btn {
  position: absolute;
  right: 18px;
  top: 18px;
  font-size: 2rem;
  color: #444;
  cursor: pointer;
  z-index: 10;
  font-weight: bold;
  background: #fff;
  border-radius: 50%;
  width: 36px;
  height: 36px;
  line-height: 36px;
  text-align: center;
  transition: background 0.2s, color 0.2s;
  box-shadow: 0 2px 8px #e0e0e0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.close-btn:hover {
  background: #8d26e6;
  color: #fff;
}
.register-title {
  color: #8d26e6;
  font-size: 2rem;
  font-weight: 700;
  text-align: center;
  margin-bottom: 28px;
  letter-spacing: 1px;
}
.register-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.register-label {
  font-size: 1.08rem;
  color: #444;
  margin-bottom: 4px;
  margin-top: 10px;
}
.register-input {
  width: 100%;
  height: 48px;
  border: 1.5px solid #e0e0e0;
  border-radius: 8px;
  padding: 0 14px;
  font-size: 1.1rem;
  color: #444;
  background: #fafaff;
  margin-bottom: 6px;
  transition: border 0.2s;
}
.register-input:focus {
  border: 1.5px solid #8d26e6;
  outline: none;
}
.register-btn {
  margin-top: 18px;
  width: 100%;
  height: 56px;
  background: #8d26e6;
  color: #fff;
  font-size: 1.35rem;
  font-weight: 700;
  border: none;
  border-radius: 28px;
  cursor: pointer;
  transition: background 0.2s;
  box-shadow: 0 2px 12px #e0e0e0;
  letter-spacing: 2px;
}
.register-btn:hover {
  background: #6d1bb3;
}
.register-footer {
  margin-top: 22px;
  text-align: center;
  color: #444;
  font-size: 1.08rem;
}
.login-link {
  color: #8d26e6;
  margin-left: 8px;
  cursor: pointer;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.2s;
}
.login-link:hover {
  color: #6d1bb3;
  text-decoration: underline;
}
@media (max-width: 600px) {
  .register-modal {
    padding: 18px 4vw 18px 4vw;
    max-width: 98vw;
    min-width: 0;
  }
  .register-title {
    font-size: 1.3rem;
  }
}
</style>
